import Sequelize from 'sequelize';
import baseDao from '../lib/baseDao.js'; // 数据库操作
import User from './user.js'
import Delivery from './delivery.js'

const Modal = baseDao.sequelize.define('t_order', {
  // 属性
  id: {
    type: Sequelize.INTEGER,
    primaryKey: true, // 主键
    autoIncrement: true, // 自增
    allowNull: true,
    validate: {
      len: [1, 50]
    }
  },
  orderNumber: {
    type: Sequelize.STRING,
    allowNull: true,
    validate: {
      len: [0, 50]
    },
    comment: '订单号'
  },
  userId: {
    type: Sequelize.INTEGER,
    allowNull: true,
    validate: {
      len: [0, 50]
    },
    defaultValue: 0,
    comment: '购买者ID'
  },
  realName: {
    type: Sequelize.STRING,
    allowNull: true,
    validate: {
      len: [0, 255]
    },
    defaultValue: "",
    comment: '真实姓名'
  },
  mobile: {
    type: Sequelize.STRING,
    allowNull: true,
    validate: {
      len: [11, 11]
    },
    comment: '手机号'
  },
  payType: {
    type: Sequelize.STRING,
    allowNull: true,
    validate: {
      len: [0, 20]
    },
    defaultValue: 'alipay',
    comment: '支付方式，cash现金支付，alipay支付宝支付，wechat微信支付，bankCard银行卡支付'
  },
  itemName: {
    type: Sequelize.STRING,
    allowNull: true,
    validate: {
      len: [0, 50]
    },
    defaultValue: '',
    comment: '商品名称'
  },
  itemMainImg: {
    type: Sequelize.STRING,
    allowNull: true,
    validate: {
      len: [0, 200]
    },
    defaultValue: '',
    comment: '商品主图'
  },
  quantity: {
    type: Sequelize.INTEGER,
    allowNull: true,
    validate: {
      len: [0, 10]
    },
    defaultValue: 1,
    comment: '购买商品数量'
  },
  singlePrice: {
    type: Sequelize.FLOAT,
    allowNull: true,
    validate: {
      len: [0, 10]
    },
    defaultValue: 0,
    comment: '商品单价'
  },
  price: {
    type: Sequelize.FLOAT,
    allowNull: true,
    validate: {
      len: [0, 10]
    },
    defaultValue: 0,
    comment: '支付金额'
  },
  deliveryLocation: {
    type: Sequelize.STRING,
    allowNull: true,
    validate: {
      len: [0, 255]
    },
    defaultValue: "",
    comment: '收货地址'
  },
  status: {
    type: Sequelize.INTEGER,
    allowNull: true,
    validate: {
      len: [0, 10]
    },
    defaultValue: 1,
    comment: '订单状态：-3已退款，-2已退货，-1退款中，1待付款，2已付款，3已发货，4已收货，5已评价'
  },
  skuAttr: {
    type: Sequelize.STRING,
    allowNull: true,
    validate: {
      len: [0, 500]
    },
    defaultValue: "{}",
    comment: '商品SKU'
  },
  userRemark: {
    type: Sequelize.STRING,
    allowNull: true,
    validate: {
      len: [0, 255]
    },
    comment: '用户备注'
  },
  sellerRemark: {
    type: Sequelize.STRING,
    allowNull: true,
    validate: {
      len: [0, 200]
    },
    comment: '销售备注'
  },
  deliveryRemark: {
    type: Sequelize.STRING,
    allowNull: true,
    validate: {
      len: [0, 200]
    },
    comment: '物流操作员备注'
  },
  supplierRemark: {
    type: Sequelize.STRING,
    allowNull: true,
    validate: {
      len: [0, 200]
    },
    comment: '供应商备注'
  },
}, {
    // 参数
  });

Modal.belongsTo(User.Modal, { foreignKey: 'userId', targetKey: 'id', as: 'buyer' });
Modal.hasOne(Delivery.Modal, { foreignKey: 'orderId', targetKey: 'id', as: 'delivery' });

//向外暴露
export default {
  Modal,
}